/*
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.apps.mytracks.clientes;

import com.doverty.android.maps.mytracks.R;
import com.google.android.apps.mytracks.AbstractMyTracksActivity;
import com.google.android.apps.mytracks.TrackListActivity;
import com.google.android.apps.mytracks.content.Cliente;
import com.google.android.apps.mytracks.content.TelefonoCliente;
import com.google.android.apps.mytracks.fragments.DeleteClienteDialogFragment.DeleteClienteCaller;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

/**
 * An activity to display cliente detail info.
 * 
 * @author Leif Hendrik Wilden
 */
public class ClienteDetailActivity extends AbstractMyTracksActivity implements DeleteClienteCaller {

  public static final String EXTRA_CLIENTE_ID = "cliente_id";
  private static final String TAG = ClienteDetailActivity.class.getSimpleName();

  private Cliente cliente;
  private TelefonoClienteDataSource dbTelefonoCliente;
  private ClienteDataSource dbCliente;
  private List<TelefonoCliente> telefonosCliente;
  private TextView clienteNombre;
  private TextView clienteTelefono1;
  private TextView clienteTelefonoTipo1;
  private TextView clienteTelefono2;
  private TextView clienteTelefonoTipo2;
  private TextView clienteTelefono3;
  private TextView clienteTelefonoTipo3;
  private TextView clienteDireccion;
  private TextView clienteEmail;


  @Override
  protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    long clienteId = getIntent().getLongExtra(EXTRA_CLIENTE_ID, -1L);
    if (clienteId == -1L) {
      Log.d(TAG, "invalid cliente id");
      finish();
      return;
    }

    dbCliente = new ClienteDataSource(this);
    dbCliente.open();
    dbTelefonoCliente = new TelefonoClienteDataSource(this);
    dbTelefonoCliente.open();
    telefonosCliente = dbTelefonoCliente.getAllTelefonoCliente(clienteId);
    cliente = dbCliente.getCliente(clienteId);
    clienteNombre = (TextView) findViewById(R.id.cliente_detail_nombre_info);
    clienteNombre.setText(cliente.getNombre());
    if (telefonosCliente.size()>0){
      findViewById(R.id.cliente_detail_telefono).setVisibility(View.VISIBLE);
      findViewById(R.id.cliente_detail_telefono_label).setVisibility(View.VISIBLE);
      findViewById(R.id.cliente_detail_telefono_info1).setVisibility(View.VISIBLE);
      telefonosCliente = dbTelefonoCliente.getAllTelefonoCliente(clienteId);
      clienteTelefono1 = (TextView) findViewById(R.id.cliente_detail_telefono_num1);
      clienteTelefono1.setText(telefonosCliente.get(0).getNumero());
      clienteTelefonoTipo1 = (TextView) findViewById(R.id.cliente_detail_telefono_tipo1);
      clienteTelefonoTipo1.setText(telefonosCliente.get(0).getTipo());
      if (telefonosCliente.size()>1){
        findViewById(R.id.cliente_detail_telefono_info2).setVisibility(View.VISIBLE);
        clienteTelefono2 = (TextView) findViewById(R.id.cliente_detail_telefono_num2);
        clienteTelefono2.setText(telefonosCliente.get(1).getNumero());
        clienteTelefonoTipo2 = (TextView) findViewById(R.id.cliente_detail_telefono_tipo2);
        clienteTelefonoTipo2.setText(telefonosCliente.get(1).getTipo());
        if (telefonosCliente.size()>2){
          findViewById(R.id.cliente_detail_telefono_info3).setVisibility(View.VISIBLE);
          clienteTelefono3 = (TextView) findViewById(R.id.cliente_detail_telefono_num3);
          clienteTelefono3.setText(telefonosCliente.get(2).getNumero());
          clienteTelefonoTipo3 = (TextView) findViewById(R.id.cliente_detail_telefono_tipo3);
          clienteTelefonoTipo3.setText(telefonosCliente.get(2).getTipo());
        }
      }
    }

    clienteDireccion = (TextView) findViewById(R.id.cliente_detail_direccion_info);
    clienteDireccion.setText(cliente.getDireccion());
    clienteEmail = (TextView) findViewById(R.id.cliente_detail_email_info);
    clienteEmail.setText(cliente.getEmail());
    
    Button btnRutas = (Button) findViewById(R.id.buttonVerRutasCliente);
    btnRutas.setOnClickListener(new View.OnClickListener() {
      @Override
        public void onClick(View v) {
          Intent intent = new Intent(getApplicationContext(), TrackListActivity.class);
          intent.putExtras(getIntent().getExtras());
          startActivity(intent);
        }
  });

  }

  @Override
  protected int getLayoutResId() {
    return R.layout.cliente_detail_activity;
  }

  @Override
  public void onDeleteClienteDone() {
    runOnUiThread(new Runnable() {
        @Override
      public void run() {
        finish();
      }
    });
  }

}